package modelLayer;
import java.io.Serializable;

/**
 * Write a description of class Sales here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sales implements Serializable
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
    private static int ii = 1;
    private Tree tree;
    private double amount;
    private double tempAmount;

    /**
     * Constructor for objects of class Sales
     * @Param Tree - The Tree Object to be Sold
     * @Param double - The amount of the Tree Object to be sold
     */
    public Sales(Tree tree, double amount){
        this.tree = tree;
        this.amount = amount;
        this.tempAmount = tree.getSquareKilometer();
        this.tree.setSquareKilometer(tree.getSquareKilometer() - amount);
        this.id = Sales.ii;
        Sales.ii++;
    }

    public Tree getTree(){
        return tree;
    }
    public void setTree(Tree tree){
        this.tree = tree;
    }
    public double getAmount(){
        return amount;
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public int getId(){
        return id;
    }
    
    public double getPrice(){
        double price = tree.getCostPerSquareKilometer() * amount;
        return price;
    }
    
    /**
     * cancel - Returns the original amount to the Tree.
     */
    public void cancel(){
        tree.setSquareKilometer(tree.getSquareKilometer() + amount);
    }
    
    /**
     * commit - Commits the Sale, removing the amount of Square Kilometers
     * If there is none left in the tree element, it is removed entierly.
     * @Return String - Information containing the Tree and the Amount sold
     */
    public String commit(){
        String s = "Tree(" + tree.getSort() + ") amount (" + amount + ")";
        
        if(tempAmount == amount){
            ForestContainer fCont = ForestContainer.getInstance();
            Forest f = fCont.containsTree(tree);
            f.removeTree(tree);
        }
        
        return s;
    }
    
    /**
     * @Overwrites
     */
    public String toString(){
        String s = "Sales(ID: " + id + " / Sort: " + tree.getSort() + " / Amount: " + amount + " / Price: " + getPrice() + ")";
        return s;
    }
}
